
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Homework5 - Info displayer</title>
        <script src="resources/js/jquery.js" type="text/javascript"></script>
        <script src="resources/js/bootstrap.min.js" type="text/javascript"></script>
        <script src="resources/js/bootstrap-datepicker.js" type="text/javascript"/></script>
    <style>
        body {
            padding: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
            padding-top: 20px;
        }
    </style>

    <link href="resources/css/bootstrap-responsive.css" rel="stylesheet">
    <link href="resources/css/style.css" rel="stylesheet">
</head>
<body>
    <h1> Homework 5 - Servlets Info Displayer </h1>



    <div class="offset3"> 
        <a class="btn btn-large btn-primary" type="btn" onclick="window.location.reload();" href=""><i class="icon-refresh"></i> &nbsp;Refresh page (F5)</a>
        <a class="btn btn-large btn-primary" type="btn" onclick="window.location.reload(true);" href=""><i class="icon-retweet"></i> &nbsp;Hard Refresh page (Crtl + F5)</a>
    </div>
    <div id="log_div">
        <jsp:include page="showLog.jsp"></jsp:include>
    </div>
    <h2>Index of content</h2>
    <ol>
        <li><a href="#variables"> Attribute and local variable definition example </a></li>
       <li> <a href="#sessions"> Session management </a></li>
       <li> <a href="#servlet_context"> Servlet context parameters</a></li>
       <li> <a href="#other_info"> More information </a> </li>
    </ol>

<section id="variables">
    <h3>Attribute and local variable definition example</h3>
    <small>Theory: </small> <p>We have defined two kind of variables - a class attribute and a local variable. This means that
        the attribute will be initialized when the init method is executed in the servlet creation process. Moreover the second variable will be initialized every time
        the servlet doPost/doGet method is executed, that means every time we send a petition to the servlet.

        Therefore we have something similar following code:
    </p><p>
        <code> 
            < %! private Integer counterA = 0 % >
        </code>
        <code>
            < % Integer counterB = 0 % >
        </code>
    </p>
    <p>
        That makes the difference between creating a local variable or a class attribute in JSP.
    </p>
    <p>We can see the results incrementing both variables and reloading the page to see that the first will have the same value as the second one will be initialized over and over</p>
    <small>Example: </small>
    <fieldset>
        <div class="offset2 span7 well">
            <form action="superJSP.jsp" method="POST">
                <label>Class Attribute:</label>
                <input id="counter" type="text" name="counter" value="1" size="3"/>  
                <label>Local variable:</label>
                <input id="counter2" type="text" name="counter2" value="1" size="3"/>
                <input class="btn btn-info offset5" type="submit" value="Increment variables"/>  
            </form>
    </fieldset>

    <script type="text/javascript"> 
        $(document).ready(function() {
            $("#counter").keypress(function(event) {
                return /\d/.test(String.fromCharCode(event.keyCode));
            });
            $("#counter2").keypress(function(event) {
                return /\d/.test(String.fromCharCode(event.keyCode));
            });
        });
    </script>  

</section>

<br/>


<section id="sessions">
    <h3>Session management</h3>
    <small>Theory: </small> <p>Listener is one of the most popular technologies used in the J2EE web application. It is part of the Java Servlet as defined in Servlet 2.3 but they have their own specific functionalities. There are two most widely used Servlet Listener i.e. ServletContextListener and HttpSessionListener.
        Let us create a Servlet Listener that just counts the number of running http sessions and prints the details whenever a session gets created or destroy.
        <br/>

    <ul>
        <li>The sessionCreated() method will be called by the servlet container whenever a new session is created for this application. An object of javax.servlet.http.HttpSessionEvent class is passed as an argument to the sessionCreated method. This object can be used to get the session related information including session ID. In our example we have used a counter sessionCounter which counts the number of live session at any given point of time. Whenever a new session is created, this count gets incremented.
        </li>
        <li>
            The sessionDestroyed() method will be called by the servlet container whenever an existing session is invalidated. We have used this method in our example to decrement the session count and display the ID of session being destroyed.
        </li>
    </ul>

    <p>    <small>Example:</small></p>


    <div class="span6 offset2 well">
        <jsp:include page="sessionCreator.jsp"></jsp:include>
    </div>
</section>

<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<section id="servlet_context">
    <h3>Servlet context parameters</h3>
    <small>Theory: </small>
    <p>
        ServletContext is an interface which helps us to communicate with the servlet container. There is only one ServletContext for the entire web application and the components of the web application can share it. The information in the ServletContext will be common to all the components. Remember that each servlet will have its own ServletConfig. The ServetContext is created by the container when the web application is deployed and after that only the context is available to each servlet in the web application.
    </p>
    <p>
    <h4>Web application initialization:</h4>
    <ol>
        <li>First of all the web container reads the deployment descriptor file and then creates a name/value pair for each <code><context-param></code> tag.</li>
        <li>After creating the name/value pair it creates a new instance of ServletContext.</li>
        <li>
            Its the responsibility of the Container to give the reference of the ServletContext to the context init parameters. </li>
        <li>The servlet and jsp which are part of the same web application can have the access of the ServletContext.
        </li>
    </ol>

    <p> The Context init parameters are available to the entire web application not just to the single servlet like servlet init parameters.
    </p>

    <small>Example: </small>
    This is how we show a init parameter declared in web.xml:
    <code>
        this.getServletContext().getInitParameter("exampleParam");
    </code>
    <p class="well">exampleParam = <b><%= getServletContext().getInitParameter("exampleParam")%></b>  </p>
    <h4>Adding attributes to the context</h4>
    <p>
        <small> Theory: </small>
        This functionality can be used for example to send information from one servlet to another servlet. 
    </p>
    <p>It's really simple to implement this functionality: </p>
    <ul>
        <li> setAttribute("String str", Object obj);

            Value in setAttribute() method is set in the key-value pair as the value is set in Map. The first argument is a string which act as a 'key' value and the second argument is an Object type variable which act as 'value' of that 'key'.
        </li>
        <li>
            getAttribute("String str");

            getAttribute() method fetches the value set in setAttribute() method. Argument of this method is act as a 'key' which contains the value and the variable should be the same as of the first argument of setAttribute() method.

        </li>
    </ul>

    <small>Example: </small>
    We set and rescue a parameter by
    <code>setAttribute("email", new String("pass@wwwtech.com"));</code>
    <code>getAttribute("email");</code>
    <p class="well">email = 
        <% this.getServletContext().setAttribute("email", new String("pass@wwwtech.com"));
        %><b><%= this.getServletContext().getAttribute("email")%></b>
    </p> 
</section> 

<section id="other_info">
    <h3>Other servlet information</h3>
    <small>Theory:</small>
    <p>Other information can be accessed from the servlet</p>
    <small>Example: </small>
    <div class="well">
        <p>Servlet name: <b><%= this.getServletName()%> </b> 
        <p>Servlet Info:  <b><%= this.getServletInfo()%> </b> 
        <p>Servlet context name: <b><%=this.getServletContext().getServletContextName()%></b> </p>
        <p>Server info: <b><%=this.getServletContext().getServerInfo()%></b> </p>
    </div>
</section>
</body>
</html>
